package com.itheima.imclient100;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.hyphenate.util.DateUtils;
import com.itheima.imclient100.event.ContactChangedEvent;
import com.itheima.imclient100.event.OfflineEvent;
import com.itheima.imclient100.utils.SPUtils;
import com.itheima.imclient100.utils.ToastUtils;
import com.itheima.imclient100.view.LoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

public class BaseActivity extends AppCompatActivity {

    private ProgressDialog dialog;
    private MyBroadcastReceiver receiver;
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiver = new MyBroadcastReceiver();
        //LocalBroadcastManager  通过这个类注册/发送的广播只在当前应用有效 不会影响到其它应用 也不会受其它应用影响
        localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());

        localBroadcastManager.registerReceiver(receiver,new IntentFilter("com.itheima.closeallactivity"));
    }

    public void showProgressDialog(String title){
        if(dialog == null){
            dialog = new ProgressDialog(this);
        }
        dialog.setMessage(title);
        dialog.show();
    }

    public void cancelProgressDialog(){
        if(dialog!=null){
            dialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(dialog!=null){
            dialog.dismiss();
            dialog = null;
        }
        localBroadcastManager.unregisterReceiver(receiver);
    }

    public  void showToast(String msg){
        ToastUtils.showToast(this,msg);
    }


    private class MyBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("com.itheima.closeallactivity")){
                finish();
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void contactChanged(OfflineEvent event){
        //收到联系人变化的消息之后 刷新界面
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("下线通知");
        builder.setMessage("您的账号于"+
                DateUtils.getTimestampString(new Date())+"在其它设备登录,如非本人操作请及时修改密码");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                localBroadcastManager.sendBroadcast(new Intent("com.itheima.closeallactivity"));
            }
        });
        builder.show();
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);

    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
